//
//  MainViewController.m
//  SinaWeibo
//
//  Created by user on 15/10/13.
//  Copyright © 2015年 ZT. All rights reserved.
//

#import "MainViewController.h"
#import "HomeViewController.h"
#import "CloudtaxViewController.h"
#import "AnswerViewController.h"
#import "SchoolViewController.h"
#import "MyViewController.h"
#import "WNavigationViewController.h"
#import "WTabBar.h"
#import "ServiceViewController.h"

#define TabbarItemNums 5.0    //tabbar的数量 如果是5个设置为5.0

@interface MainViewController ()<WTabBarDelegate>

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 添加子控制器
    [self addChildVc:[[HomeViewController alloc] init] title:@"首页" image:@"SY_WXZ@2x.png" selectedImage:@"SY_XZ@2x.png"];
    
    [self addChildVc:[[ServiceViewController alloc] init] title:@"服务" image:@"FW_WXZ@2x.png" selectedImage:@"FW_XZ@2x.png"];
    
    [self addChildVc:[[AnswerViewController alloc] init] title:@"" image:@"" selectedImage:@""];

    [self addChildVc:[[CloudtaxViewController alloc] init] title:@"云税" image:@"YS_WXZ@2x.png" selectedImage:@"YS_XZ@2x.png"];
    
    [self addChildVc:[[MyViewController alloc] init] title:@"我的" image:@"WD_WXZ@2x.png" selectedImage:@"WD_XZ@2x.png"];

//  [self showBadgeOnItemIndex:1];

//    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WSCREENH_WIDTH, 0.3)];
//    line.backgroundColor = WRGBColor(113, 102, 155);
//    [self.tabBar.layer addSublayer:line.layer];

    //创建自定义TabBar
    WTabBar *wTabBar = [[WTabBar alloc] init];
    wTabBar.WTabBarDelegate = self;

    //利用KVC替换默认的TabBar
    [self setValue:wTabBar forKey:@"tabBar"];

}

/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字(可以设置tabBar和navigationBar的文字)
    childVc.title = title;
    
    // 设置子控制器的tabBarItem图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    // 禁用图片渲染
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 设置文字的样式
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : WRGBColor(123, 123, 123)} forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:0.15 green:0.62 blue:0.99 alpha:1]} forState:UIControlStateSelected];
    
    if ([title isEqualToString:@""]) {
        childVc.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
    }

    if ([title isEqualToString:@"朋友"]) {
//        childVc.tabBarItem.badgeValue = @"";
    }

//    childVc.view.backgroundColor = RandomColor; // 这句代码会自动加载主页，消息，发现，我四个控制器的view，但是view要在我们用的时候去提前加载
    
    // 为子控制器包装导航控制器
    WNavigationViewController *navigationVc = [[WNavigationViewController alloc] initWithRootViewController:childVc];
    // 添加子控制器
    [self addChildViewController:navigationVc];

}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

    if ([item.title isEqualToString:@"首页"]) {
        [WShareUtil sharedData].tabbarIndex = 0;
    } else if ([item.title isEqualToString:@"服务"]) {
        [WShareUtil sharedData].tabbarIndex = 1;
    } else if ([item.title isEqualToString:@"云税"]) {
        [WShareUtil sharedData].tabbarIndex = 3;
    } else if ([item.title isEqualToString:@"我的"]) {
        [WShareUtil sharedData].tabbarIndex = 4;
    }

    WTabBar *wtabBar = (WTabBar *)self.tabBar;
//    [wtabBar.plusBtn setBackgroundImage:[UIImage imageNamed:@"WB_WXZ@2x.png"] forState:UIControlStateNormal];
    [wtabBar.plusBtn setImage:[UIImage imageNamed:@"WB_WXZ@2x.png"] forState:UIControlStateNormal];
}

/**
 *  加号按钮点击
 */
- (void)tabBarDidClickPlusButton:(WTabBar *)tabBar
{
    self.selectedIndex = 2;
    [WShareUtil sharedData].tabbarIndex = 2;
}

//显示小红点
- (void)showBadgeOnItemIndex:(int)index{
    //移除之前的小红点
    [self removeBadgeOnItemIndex:index];
    
    //新建小红点
    UIView *badgeView = [[UIView alloc]init];
    badgeView.tag = 888 + index;
    badgeView.layer.cornerRadius = 5;//圆形
    badgeView.backgroundColor = [UIColor redColor];//颜色：红色
    CGRect tabFrame = self.tabBar.frame;
    
    //确定小红点的位置
    float percentX = (index + 0.7) /TabbarItemNums;
    CGFloat x = ceilf(percentX * tabFrame.size.width);
    CGFloat y = ceilf(0.1 * tabFrame.size.height);
    badgeView.frame = CGRectMake(x, y, 10, 10);//圆形大小为10
    [self.tabBar addSubview:badgeView];
}

//隐藏小红点
- (void)hideBadgeOnItemIndex:(int)index{
    //移除小红点
    [self removeBadgeOnItemIndex:index];
}

//移除小红点
- (void)removeBadgeOnItemIndex:(int)index{
    //按照tag值进行移除
    for (UIView *subView in self.tabBar.subviews) {
        if (subView.tag == 888+index) {
            [subView removeFromSuperview];
        }
    }
}

@end
